﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Liang.DataBase.Tools
{
    /// <summary>
    /// SQLite工具类
    /// </summary>
    public class SQLiteTool<T> where T : class, new()
    {
        /// <summary>
        /// 打包数据（如果有分包，则包名按 文件名_N 递增，N 从 1 开始）
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="path">存储路径</param>
        /// <param name="fileName">文件名（请勿带后缀）</param>
        /// <param name="srcData">源数据</param>
        /// <param name="packageSize">包大小</param>
        /// <returns></returns>
        public static bool PackageData(string path, string fileName, List<T> srcData, int packageSize = 0)
        {
            if (0 == srcData.Count || 0 > packageSize)
            {
                return false;
            }

            try
            {
                if (0 == packageSize)
                {
                    using (SQLiteEx.SQLiteEx<T> sqliteEx = new SQLiteEx.SQLiteEx<T>())
                    {
                        if (!sqliteEx.OpenDB(string.Format("{0}{1}.db", path, fileName)))
                        {
                            return false;
                        }

                        if (!sqliteEx.Insert(srcData))
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    // 当前包编号
                    int nPackageIndex = 1;
                    // 剩余还需要拷贝的记录数
                    int nLeftCount = srcData.Count;
                    int nToCopyed = 0;

                    while (0 < nLeftCount)
                    {
                        if (0 < (nLeftCount - packageSize))
                        {
                            nToCopyed = packageSize;
                        }
                        else
                        {
                            nToCopyed = nLeftCount;
                        }

                        // 拷贝到临时数组
                        T[] tmpRec = new T[nToCopyed];
                        srcData.CopyTo((nPackageIndex - 1) * packageSize, tmpRec, 0, nToCopyed);

                        // 创建一个新的数据库，并写入数据
                        using (SQLiteEx.SQLiteEx<T> sqliteEx = new SQLiteEx.SQLiteEx<T>())
                        {
                            if (sqliteEx.OpenDB(string.Format("{0}{1}_{2}.db", path, fileName, nPackageIndex)))
                            {
                                if (!sqliteEx.Insert(tmpRec))
                                {
                                    return false;
                                }
                            }
                        }

                        nLeftCount -= nToCopyed;
                        nPackageIndex++;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return true;
        }

        /// <summary>
        /// 加载指定文件或路径下所有文件的表 T 的数据
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<T> RetrieveData(string path)
        {
            List<T> ret = new List<T>();

            if (File.Exists(path))
            {
                // 文件
                using (SQLiteEx.SQLiteEx<T> sqliteEx = new SQLiteEx.SQLiteEx<T>())
                {
                    if (sqliteEx.OpenDB(path, false))
                    {
                        ret = sqliteEx.Query();
                    }
                }
            }
            else if (Directory.Exists(path))
            {
                // 文件夹
            }

            return ret;
        }
    }
}
